package com.byit.controller;

import com.alibaba.fastjson.JSONObject;
import com.byit.builder.MapBuilder;
import com.byit.domain.AppApplictionMenu;
import com.byit.domain.AppUserAppliction;
import com.byit.enums.PromptInfo;
import com.byit.jwt.ann.JwtUser;
import com.byit.jwt.domain.JwtSession;
import com.byit.msg.MessageResult;
import com.byit.service.IAppApplicationService;
import com.byit.service.IAppApplictionMenuService;
import com.byit.service.IAppUserApplictionService;
import com.byit.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by Administrator on 2019/1/4.
 */

@RestController
@RequestMapping("/app/appliction")
public class AppApplicationController {


    @Autowired
    private IAppApplicationService applicationService;
    @Autowired
    private IAppApplictionMenuService applictionMenuService;
    @Autowired
    private IAppUserApplictionService appUserApplictionService;


    /**
     * 功能点击次数累加
     *
     * @param id
     * @return
     */
    @PostMapping("/click/add")
    public Object clickCount(@RequestParam("id") Long id) {
        AppApplictionMenu applictionMenu = applictionMenuService.selectById(id);
        applictionMenu.setClickCount(applictionMenu.getClickCount() + 1);
        applictionMenuService.updateById(applictionMenu);
        return MessageResult.success();
    }

    /**
     * 用户未添加的功能列表
     *
     * @return
     */
    @GetMapping("/all")
    public Object all(@JwtUser JwtSession session, @RequestParam("appIds") String appIds) {
        List<JSONObject> list = new ArrayList<JSONObject>();
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(appIds)) {
            List<Long> idList = StringUtil.splitLong(appIds, ",");
            map.put("idList", idList);
        }

        if (!session.isLogin()) {
            list = applicationService.selectByApp(map);
        } else {
            Long userId = session.userId();
            map.put("userId", userId);
            list = appUserApplictionService.selectNotAddMenus(map);
        }

        return MessageResult.success(list);
    }

    /**
     * 我的应用编辑
     *
     * @param ids
     * @param session
     * @return
     */
    @PostMapping("/add/myApplication")
    public Object addMyApplication(@RequestParam("ids") String ids, @JwtUser JwtSession session) {
        if (!session.isLogin()) {
            return MessageResult.warpper(PromptInfo.USER_LOGIN_INVALID);
        }
        Long userId = session.userId();

        List<Long> idList = StringUtil.splitLong(ids, ",");
        if (idList.size() > 8) {
            return MessageResult.error("应用个数不能大于8个！");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("user_id", userId);
        boolean b = appUserApplictionService.deleteByMap(map);
        if (!b) {
            return MessageResult.error("编辑失败");
        }
        Integer i = 0;
        for (Long id : idList) {
            i++;
            AppUserAppliction userAppliction = new AppUserAppliction();
            userAppliction.setAppId(id);
            userAppliction.setUserId(userId);
            userAppliction.setCreateTime(new Date());
            userAppliction.setUpdateTime(new Date());
            userAppliction.setSort(i);
            appUserApplictionService.insert(userAppliction);
        }

        return MessageResult.success("编辑成功");
    }

    /**
     * 所有分类列表
     *
     * @return
     */
    @GetMapping("/search")
    public Object all(@RequestParam(value = "keyword", required = false) String keyword) {
        if (StringUtils.isBlank(keyword)) { // 数据校准
            keyword = "";
        }

        List<JSONObject> menus = applictionMenuService.selectAllApplication(MapBuilder.newMap(1)
                .setParam("keyword", '%' + StringUtil.decodeSpecialCharsWhenLikeUseBackslash(keyword) + '%')
                .ok());

        return MessageResult.success(menus);
    }


}
